<?php
/**
 * Created by PhpStorm.
 * User: dungnt13
 * Date: 12/5/14
 * Time: 11:51 AM
 */

namespace App\Services;

use Cache,
    Category,
    Topic,
    DB,
    Question,
    Vcategory,
    Tone;

class ContentsServices extends BaseService
{
    protected $timeCache = 60;

    public function categoriesRightMenu()
    {
        $key = 'categories_menu_lesson';
        $categories = array();
        if (Cache::has($key)) {
            $categories = Cache::get($key);
        } else {
            $categories = Category::where('type', '=', 'lesson')->where('parent_id', '==', 0)->where('status', '=', 'on')
                ->select('id', 'title', 'slug')->get();
            Cache::put($key, $categories, 60);
        }
        return $categories;
    }

    public function topicsRightMenu()
    {
        $key = 'topics_right_menu';
        $topics = array();
        if (Cache::has($key))
            $topics = Cache::get($key);
        else {
            $topics = \Topic::where('status', '=', 'on')->get();
            Cache::put($key, $topics, $this->timeCache);
        }
        return $topics;
    }

    public function tonesDropdown()
    {
        $key = 'tones_dropdown_menu';
        $tones = array();
        if (Cache::has($key))
            $tones = Cache::get($key);
        else {
            $tones = Tone::where('status', '=', 'on')->get();
            Cache::put($key, $tones, $this->timeCache);
        }
        return $tones;
    }

    public function tonesRightMenu()
    {
        $key = 'tones_right_menu';
        $tones = array();
        if (Cache::has($key))
            $tones = Cache::get($key);
        else {
            $tones['videos'] = Tone::leftJoin('videos', 'tones.id', '=', 'videos.tone_id')->select('tones.*', DB::raw("count(videos.id) as total"))->groupBy('tones.id')->get();
            $tones['lessons'] = Tone::leftJoin('lessons', 'lessons.tone_id', '=', 'tones.id')->select('tones.*', DB::raw("count(lessons.id) as total"))->groupBy('tones.id')->get();
            $tones['lyrics'] = Tone::leftJoin('lyrics', 'lyrics.tone_id', '=', 'tones.id')->select('tones.*', DB::raw("count(lyrics.id) as total"))->groupBy('tones.id')->get();
            Cache::put($key, $tones, $this->timeCache);
        }
        return $tones;
    }

    public function getVideoCategories()
    {
        $key = 'video_categories';
        $categories = array();
        if (Cache::has($key))
            $categories = Cache::get($key);
        else {
            $categories = Vcategory::where('status', '=', 'on')->get();
            Cache::put($key, $categories, $this->timeCache);
        }
        return $categories;
    }

    public function getTopQuestions($data = array())
    {
        $questions = Question::where('type', '=', 'full');
        if (isset($data['category']))
            $questions = $questions->where('category_id', '=', $data['category']);
        $questions = $questions->take(10)->orderBy('views', 'DESC')->get();
        return $questions;
    }

    public function listLyrics($data = array())
    {
//         $lyrics = Lyric::
    }
} 